package com.mondo.financialwallet.presentation.ui.activity;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.common.ui.BaseActivity;
import com.example.common.utils.TimeCount;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Order;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.params.SendMessageParams;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.LocalConfigStorage;
import com.mondo.financialwallet.presentation.utils.Toasts;

import java.util.List;

import butterknife.Bind;

/**
 * 绑定邮箱
 */
public class BindEmailActivity extends BaseActivity {

    @Order(1)
    @Bind(R.id.account_edit)
    @NotEmpty(messageResId = R.string.message_please_enter_email)
    EditText mAccountEdit;

    @Order(2)
    @Bind(R.id.auth_code_edit)
    @NotEmpty(messageResId = R.string.message_please_auth_code)
    EditText mAuthCodeEdit;

    @Bind(R.id.sure_button) Button mSureButton;
    @Bind(R.id.send_verification_code) TextView mSendVerificationCode;


    private ProgressDialog mProgressDialog;
    private Validator.ValidationListener mBindEmailValidationListener =
            new Validator.ValidationListener() {

                @Override
                public void onValidationSucceeded() {
                    requestBindEmail(mAccountEdit.getText(), mAuthCodeEdit.getText());
                }

                @Override
                public void onValidationFailed(List<ValidationError> errors) {
                    if (!errors.isEmpty()) {
                        Toasts.warning(errors.get(0).getCollatedErrorMessage(
                                BindEmailActivity.this));
                    }
                }
            };

    @Override
    protected int getBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setWhiteDarkColorStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_bind_email;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_bind_email);

        // 发送验证码
        mSendVerificationCode.setOnClickListener(v ->
                requestEmailCode(SendMessageParams.email(mAccountEdit.getText())));

        // 绑定邮箱
        Validator validator = new Validator(this);
        validator.setValidationListener(mBindEmailValidationListener);
        mSureButton.setOnClickListener(v -> requestBindEmail(
                mAccountEdit.getText(),
                mAuthCodeEdit.getText()
        ));
    }

    private void requestBindEmail(CharSequence email, CharSequence code) {
        DataRepository.get().bindEmail(email.toString(), code.toString(), result -> {
            if (result.isSuccess()) {
                finish();
                Toasts.success(R.string.message_bind_success);
                LocalConfigStorage.get().bindEmail(true);
            } else {
                Toasts.failure(result.getMsg());
            }
        });
    }

    private void requestEmailCode(SendMessageParams params) {
        if (TextUtils.isEmpty(params.getValue())) {
            Toasts.warning(R.string.message_please_enter_email);
            return;
        }

        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(this, null,
                    getString(R.string.message_sending_sms_verification_code));

            DataRepository.get().sendMessageCode(params, result -> {
                mProgressDialog.dismiss();
                mProgressDialog = null;

                if (result.isSuccess()) {
                    new TimeCount(mSendVerificationCode).start();
                } else {
                    Toasts.failure(result.getMsg());
                }
            });
        }
    }
}
